package com.tz.crm.service.impl;


import com.tz.crm.dao.ContractDao;
import com.tz.crm.entity.Contract;
import com.tz.crm.service.ContractService;
import com.tz.crm.utils.Pager;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 合同服务类
 */
@Service
@Transactional
@SuppressWarnings("unchecked")
public class ContractServiceImpl extends BaseServiceImpl<Contract, String> implements ContractService {

    @Autowired
    private ContractDao contractDao;

    /**
     * 有参构造
     *
     * @param contractDao 注入的组件
     */
    @Autowired
    public ContractServiceImpl(ContractDao contractDao) {
        this.contractDao = contractDao;
        super.setBaseDao(contractDao);
    }

    @Override
    public Contract getByNum(String contractNum) {
        return contractDao.getByNum(contractNum);
    }

    @Override
    public int getCount(Contract contract) {
        return contractDao.getCount(contract);
    }

    @Override
    public Pager<Contract> search(Pager pager, Contract contract) {
        DetachedCriteria dc = DetachedCriteria.forClass(Contract.class);
        dc.add(Restrictions.eq("isdistroy", contract.isIsdistroy()));
        if (StringUtils.isNotBlank(contract.getInNum())) {
            dc.add(Restrictions.like("inNum", "%" + contract.getInNum().trim() + "%"));
        }
        if (StringUtils.isNotBlank(contract.getCustomerName())) {
            dc.add(Restrictions.like("customerName", "%" + contract.getCustomerName().trim() + "%"));
        }
        if (contract.getContractTemplate() != null && StringUtils.isNotBlank(contract.getContractTemplate().getId())) {
            dc.add(Restrictions.like("contractTemplate.id", contract.getContractTemplate().getId()));
        }
        if (contract.getSignUser() != null && StringUtils.isNotBlank(contract.getSignUser().getId())) {
            dc.add(Restrictions.like("signUser.id", contract.getSignUser().getId()));
        }
        /*Pager pager1 = contractDao.findByPager(pager,dc);
        return pager1;*/
        return contractDao.findByPager(pager, dc);
    }

 /*   public Pager select(Pager pager,Contract contract) {
        DetachedCriteria dc = DetachedCriteria.forClass(Payment.class);
//        dc.add(Restrictions.eq("isdistroy", contract.isIsdistroy()));
//        Criteria criteria = getSession().createCriteria(Payment.class,"p");
        if (StringUtils.isNotBlank(contract.getInNum())){
//            dc.createAlias("Payment.contract","c");
            dc.add(Restrictions.like("contract.inNum", "%" + contract.getInNum().trim() + "%"));
        }
        if (StringUtils.isNotBlank(contract.getCustomerName())){
            dc.add(Restrictions.eq("contract.customerName",contract.getCustomerName()));
        }
        if (StringUtils.isNotBlank(contract.getSignUser().getId())){
            dc.createAlias("contract.signUser","s");
            dc.add(Restrictions.eq("s.id",contract.getSignUser().getId()));
        }
        return contractDao.findByPager(pager, dc);
    }*/


    @Override
    public void invalid(String id) {
        Contract c = contractDao.get(id);
        c.setIsdistroy(true);
        contractDao.update(c);
    }



}
